<template>
	<view class="">
		<u-upload
			:fileList="fileList1"
			@afterRead="afterRead"
			@delete="deletePic"
			name="1"
			multiple
			width="80"
			height="80"
			:maxCount="maxCount"
		></u-upload> 
	</view>
</template>

<script>
	import IMG_URL from "@/common/config.js";
	export default {
		props:{
			maxCount:Number
		},
		data() {
			return {
				fileList1: [],
				images:IMG_URL
			}
		},
		onLoad() {
	
		},
		methods: {
			uploadUrl(content){
				this.fileList1 = content;
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.$emit('setleaveUnusedImage',this.fileList1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					if(result){
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						}))
						fileListLen++
					}
				}
				this.$emit('setleaveUnusedImage',this.fileList1);
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://118.193.37.29:8777/wms-api/common/upload',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header:{
							'Authorization':uni.getStorageSync("vuex_token")
						},
						success: (res) => {
							console.log(JSON.parse(res.data).url);
							setTimeout(() => {
								resolve(JSON.parse(res.data).url)
							}, 1000)
						}
					});
				})
			},
			aaaaaa(){
				this.fileList1 = [];
			}
		}
	}
</script>

<style>
</style>